package com.henghe.data_process.controller;

import com.alibaba.excel.EasyExcel;
import com.henghe.data_process.entity.ExcelObj;
import com.henghe.data_process.entity.ExcelObj2;
import com.henghe.data_process.service.CommonService;
import com.henghe.data_process.service.MaterialService;
import com.henghe.frame.common.entity.JsonResult;
import com.henghe.frame.domain.base.BaseClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2025/5/29 10:26
 */
@RestController
@Slf4j
public class CommonController extends BaseClass {

    @Autowired
    private CommonService commonService;
    @Autowired
    private MaterialService materialService;

    @GetMapping("supplier")
    public JsonResult getList(){
        return succListMsg(commonService.getList());
    }
//
//    @GetMapping("update")
//    public JsonResult update(){
//        List<TBdSupplier> list = commonService.getList();
//        List<TBdSupplier> collect = list.stream().filter(e -> e.getFnumber().equals("230107")).collect(Collectors.toList());
//        commonService.update(collect.get(0));
//        return succMsg();
//    }

    /**
     * 导入excel 处理供应商数据
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("import")
    public JsonResult importExcel(MultipartFile file) throws IOException {
        List<ExcelObj> objects = EasyExcel.read(file.getInputStream()).head(ExcelObj.class).sheet(0).headRowNumber(3).doReadSync();
        return succMsgData(commonService.importExcel(objects.stream().filter(e->!e.getNo().equals("*编码")).collect(Collectors.toList())));
    }

    /**
     * 导入新旧关系，处理供应商数据 废弃
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("import2")
    @Deprecated
    public JsonResult importExcel2(MultipartFile file) throws IOException {
        List<ExcelObj2> objects = EasyExcel.read(file.getInputStream()).head(ExcelObj2.class).sheet(0).doReadSync();
        commonService.importExcel2(objects);
        return succMsg();
    }

    /**
     * 修改所有暂存数据 为 其他供应商
     * @return
     */
    @GetMapping("updateStatus")
    public JsonResult updateStatus(){
        commonService.updateStatus();
        return succMsg();
    }

    /**
     * 把不为Sup开头的编码 依次排序改为Sup开头的编码
     * @return
     */
    @GetMapping("handleData")
    public JsonResult handleData(){
        commonService.handleData();
        return succMsg();
    }

    /**
     * 联系人 同步给tSecUser表
     * @return
     */
    @GetMapping("handleDataOut")
    public JsonResult handleDataOut(){
        commonService.handleDataOut();
        return succMsg();
    }

    @GetMapping("getTest")
    public JsonResult getTest(){
        return succListMsg(commonService.getTest());
    }


    /**
     * 物料相关
     */
    @GetMapping("material/test")
    public JsonResult materialTest(){
        return succListMsg(materialService.getList());
    }

    @GetMapping("material/handle")
    public JsonResult handleMaterialData(){
        materialService.handleMaterialData();
        return succMsg();
    }
}
